﻿Imports System.Data.OleDb


Public Class UserProfileUpdate
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim sql As String = "SELECT * FROM UserProfile WHERE Username=@f19"

        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
        conn.Open()

        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)

        cmd.Parameters.AddWithValue("@f19", User.Identity.Name)

        Dim datareader = cmd.ExecuteReader()


        datareader.Read()




        txttitle.Text = datareader("Title")
        txtfirstname.Text = datareader("FirstName")
        txtlastname.Text = datareader("LastName")
        lbldob.Text = "DD/MM/YYYY" + Format(datareader("DOB"))
        txtaddress1.Text = datareader("Address1")
        txtaddress2.Text = datareader("Address2")
        txttown.Text = datareader("Town")
        txtcity.Text = datareader("City")
        txtpostcode.Text = datareader("Postcode")
        txttelephoneno.Text = datareader("TelNo")
        txtdescription.Text = datareader("AboutMe")
        txtexperience.Text = datareader("X6CarsDriven")
        txtfavcarcolour.Text = datareader("FavouriteColour")
        txtmaxspeeddriven.Text = datareader("MaxSpeedDriven")
        txtfuel.Text = datareader("PreferedFuel")
        txtlitre.Text = datareader("PreferedLitre")

    End Sub

    Protected Sub btnsubmit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnsubmit.Click
        Dim newFileName As String
        newFileName = Guid.NewGuid().ToString() & getsuffix(fupprofilepicture.FileName)
        If fupprofilepicture.HasFile Then
            fupprofilepicture.SaveAs(Server.MapPath("UserProfilePicture") & "/" & newFileName)

        End If



        Dim sql As String = "UPDATE UserProfile SET Title=@f2, FirstName=@f3, LastName=@f4, DOB=@f5, Address1=@f6, Address2=@f7, Town=@f8, City=@f9, Postcode=@f10, TelNo=@f11, AboutMe=@f12, X6CarsDriven=@f13, FavouriteColour=@f14, MaxSpeedDriven=@f15, PreferedFuel=@f16, PreferedLitre=@f17, UserImage=@f18 WHERE Username=@f19"

        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
        conn.Open()

        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)

        cmd.Parameters.AddWithValue("@f2", txttitle.Text)
        cmd.Parameters.AddWithValue("@f3", txtfirstname.Text)
        cmd.Parameters.AddWithValue("@f4", txtlastname.Text)
        cmd.Parameters.AddWithValue("@f5", txtage1.Text + "/" + txtage2.Text + "/" + txtage3.Text)
        cmd.Parameters.AddWithValue("@f6", txtaddress1.Text)
        cmd.Parameters.AddWithValue("@f7", txtaddress2.Text)
        cmd.Parameters.AddWithValue("@f8", txttown.Text)
        cmd.Parameters.AddWithValue("@f9", txtcity.Text)
        cmd.Parameters.AddWithValue("@f10", txtpostcode.Text)
        cmd.Parameters.AddWithValue("@f11", txttelephoneno.Text)
        cmd.Parameters.AddWithValue("@f12", txtdescription.Text)
        cmd.Parameters.AddWithValue("@f13", txtexperience.Text)
        cmd.Parameters.AddWithValue("@f14", txtfavcarcolour.Text)
        cmd.Parameters.AddWithValue("@f15", txtmaxspeeddriven.Text)
        cmd.Parameters.AddWithValue("@f16", txtfuel.Text)
        cmd.Parameters.AddWithValue("@f17", txtlitre.Text)
        cmd.Parameters.AddWithValue("@f18", "UserProfilePicture/" & newFileName)
        cmd.Parameters.AddWithValue("@f19", User.Identity.Name)

        Dim UserDOB As String = txtage1.Text + txtage2.Text + txtage3.Text

        cmd.ExecuteNonQuery()
        conn.Close()
        cmd.Dispose()
        conn.Dispose()

        Response.Redirect("/UserProfile.aspx")


    End Sub

    Function getsuffix(ByVal fn As String)
        Dim fileparts = fn.Split(".")
        Dim suffix = fileparts(fileparts.Length - 1)
        Return "." & suffix
    End Function

End Class